﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using MethodWorx.CMS.TemplateParser.Installer;
using Microsoft.VisualStudio.TestTools.UnitTesting;


namespace MethodWorx.CMS.UI.Tests.Installer
{
    [TestClass]
    public class InstallerParserTests
    {
        [TestMethod]
        public void EnsureErrorOccursWhenNoInstallerIsPresent()
        {
            Dictionary<string, byte[]> files = new Dictionary<string, byte[]>();
            var parser = new InstallerParser(files, null);
            Assert.IsTrue(parser.Errors.Contains("Missing installer.xml"));
        }

        [TestMethod]
        public void EnsureNoErrorWhenInstallerIsPresent()
        {
            Dictionary<string, byte[]> files = new Dictionary<string, byte[]>();
            files.Add("installer.xml", System.Text.ASCIIEncoding.ASCII.GetBytes("<template/>"));
            var parser = new InstallerParser(files, null);
            Assert.IsFalse(parser.Errors.Contains("Missing installer.xml"));
        }

        [TestMethod]
        public void EnsureTypeIsMasterTemplate()
        {
            Dictionary<string, byte[]> files = new Dictionary<string, byte[]>();
            files.Add("installer.xml", System.Text.ASCIIEncoding.ASCII.GetBytes("<template type=\"MasterPageTemplate\"/>"));
            var parser = new InstallerParser(files, null);

            Assert.IsTrue(parser.InstallerType == InstallerType.MasterTemplate);
        }

        [TestMethod]
        public void EnsureErrorExistsWhenTypeIsUnknown()
        {
            Dictionary<string, byte[]> files = new Dictionary<string, byte[]>();
            files.Add("installer.xml", System.Text.ASCIIEncoding.ASCII.GetBytes("<template type=\"Unknown\"/>"));
            var parser = new InstallerParser(files, null);

            Assert.IsTrue(parser.Errors.Contains("Unknown installer type: 'Unknown'"));
        }

        [TestMethod]
        public void EnsureWizardStepsAreReadWithIntro()
        {
            Dictionary<string, byte[]> files = new Dictionary<string, byte[]>();
            files.Add("installer.xml",
                      System.Text.ASCIIEncoding.ASCII.GetBytes("<template type=\"MasterPageTemplate\"><Wizard><Step><Intro>This Is An Intro</Intro></Step></Wizard></template>"));
            var parser = new InstallerParser(files, null);

            Assert.IsTrue(parser.WizardSteps.Length == 1);
            Assert.IsTrue(parser.WizardSteps[0].Intro == "This Is An Intro");
        }

        [TestMethod]
        public void EnsureWizardStepsAreReadWithoutIntro()
        {
            Dictionary<string, byte[]> files = new Dictionary<string, byte[]>();
            files.Add("installer.xml",
                      System.Text.ASCIIEncoding.ASCII.GetBytes("<template type=\"MasterPageTemplate\"><Wizard><Step></Step></Wizard></template>"));
            var parser = new InstallerParser(files, null);

            Assert.IsTrue(parser.WizardSteps.Length == 1);
            Assert.IsTrue(string.IsNullOrEmpty(parser.WizardSteps[0].Intro));
        }

        [TestMethod]
        public void EnsureWizardStepsPromptsAreRead()
        {
            Dictionary<string, byte[]> files = new Dictionary<string, byte[]>();
            files.Add("installer.xml",
                      System.Text.ASCIIEncoding.ASCII.GetBytes("<template type=\"MasterPageTemplate\"><Wizard><Step><Ask var=\"var\" default=\"default\">This is a prompt</Ask></Step></Wizard></template>"));
            var parser = new InstallerParser(files, null);

            Assert.IsTrue(parser.WizardSteps.Length == 1);
            Assert.IsTrue(parser.WizardSteps[0].Prompts.Length == 1);
            Assert.IsTrue(parser.WizardSteps[0].Prompts[0].Prompt == "This is a prompt");
            Assert.IsTrue(parser.WizardSteps[0].Prompts[0].Variable == "var");
            Assert.IsTrue(parser.WizardSteps[0].Prompts[0].Value == "default");
        }

        [TestMethod]
        public void EnsureWorkflowStepsAreRead()
        {
            Dictionary<string, byte[]> files = new Dictionary<string, byte[]>();
            files.Add("installer.xml",
                      System.Text.ASCIIEncoding.ASCII.GetBytes("<template type=\"MasterPageTemplate\"><Workflow><AWorkflowStep><WithSomethingAsAChild></WithSomethingAsAChild></AWorkflowStep></Workflow></template>"));
            var parser = new InstallerParser(files, null);

            Assert.IsTrue(parser.WorkflowSteps.Length == 1);
            Assert.IsTrue(parser.WorkflowSteps[0].Name == "AWorkflowStep");
            Assert.IsTrue(parser.WorkflowSteps[0].Configuration.OuterXml == "<AWorkflowStep><WithSomethingAsAChild></WithSomethingAsAChild></AWorkflowStep>");
        }

    }
}
